# coding=utf-8
import json

from common.notification.templates import (
    REWARD_CHECKING, REWARD_CHECKED, WITHDRAW_SUCCESS_TEMPLATE, REFUND_TEMPLATE, WITHDRAW_BACK_TEMPLATE,
    VERIFY_SUCCESSFULLY, AWARD_SUCCESSFULLY, RECHARGE_SUCCESSFULLY, WINNER_RANKING_AWARD_DAILY,
    WINNER_RANKING_AWARD_WEEKLY, WINNER_RANKING_AWARD_TOTAL, OFFLINE_RECHARGE_SUCCESSFULLY,
    OFFLINE_RECHARGE_FAIL, OFFLINE_RECHARGE_APPLY, REPLY_FEEDBACK)
from common.notification.model import NOTIFY_TYPE
from common.notification import db as notification_db
from common.cache import redis_cache
from common.lottery.cyclical import ORDER_STATUS
from common.utils.exceptions import ServerError


def notify_win(user_id, order_status, info):
    notify_type = NOTIFY_TYPE.AWARD
    if order_status == ORDER_STATUS.WINNED:
        template = REWARD_CHECKED
        redis_cache.increase_unread_notify_count(user_id, notify_type)
    else:
        template = REWARD_CHECKING
    generate_user_notification(user_id, notify_type, template, info)


def notify_withdraw_success(user_id, info):
    template = WITHDRAW_SUCCESS_TEMPLATE
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_award_success(user_id, campaign_name, game_type, price):
    template = AWARD_SUCCESSFULLY
    info = {
        'campaign': campaign_name,
        'type': game_type,
        'price': price
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_offline_pay_apply(user_id, price):
    template = OFFLINE_RECHARGE_APPLY
    info = {
        'price': price,
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_offline_pay_fail(user_id, price, reason='未知'):
    template = OFFLINE_RECHARGE_FAIL
    info = {
        'price': price,
        'reason': reason
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_feedback(user_id, reply_content, feedback_content, created_at):
    template = REPLY_FEEDBACK
    info = {
        'reply_content': reply_content,
        'feedback_content': feedback_content,
        'created_at': created_at
    }
    return generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_offline_pay_success(user_id, price):
    template = OFFLINE_RECHARGE_SUCCESSFULLY
    info = {
        'price': price
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_winner_rank_daily(user_id, date, rank, price):
    template = WINNER_RANKING_AWARD_DAILY
    info = {
        'date': date,
        'rank': rank,
        'price': price
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_winner_rank_weekly(user_id, rank, price):
    template = WINNER_RANKING_AWARD_WEEKLY
    info = {
        'rank': rank,
        'price': price
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_winner_rank_total(user_id, rank, price):
    template = WINNER_RANKING_AWARD_TOTAL
    info = {
        'rank': rank,
        'price': price
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_recharge_success(user_id, campaign_name, price):
    template = RECHARGE_SUCCESSFULLY
    info = {
        'campaign': campaign_name,
        'price': price
    }
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_withdraw_back(user_id, info):
    template = WITHDRAW_BACK_TEMPLATE
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_refund(user_id, info):
    template = REFUND_TEMPLATE
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def notify_verify_successfully(user_id, info):
    template = VERIFY_SUCCESSFULLY
    generate_user_notification(user_id, NOTIFY_TYPE.INSIDE, template, info)


def generate_user_notification(user_id, notify_type, template, info, extend=None):
    if not user_id:
        raise ServerError('user_id is None')
    content = {}
    content['title'] = template['title'].format(**info)
    content['body'] = template['body'].format(**info)
    content['tag'] = template['tag']
    content = json.dumps(content, ensure_ascii=False)
    return notification_db.create_user_notification(user_id, notify_type, content)
